﻿using StockDataBase;
using StockDataBase.models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using 股票数据;

namespace Download
{
    public class DownStockInfos
    {
        private static DownStockInfos down = new DownStockInfos();
        private DownStockInfos()
        {
        }

        public static DownStockInfos Instance
        {
            get { return down; }
        }

        private void down_hongkong_stock()
        {
            DownBase db = new DownBase();
            string[] lines = null;
            {
                var content = "";
                int segment = 50;
                for (int j = 0; j < 4000 / segment; j++)
                {
                    var all = @"http://qt.gtimg.cn/q=";
                    for (int i = 1; i <= segment; i++)
                    {
                        all += string.Format("r_hk{0:00000},", j * segment + i);
                    }
                    var line = db.DownString(all);
                    content += line;
                }
                lines = content.Split(new char[] { ';' });
            }
            foreach (var line in lines)
            {
                Regex regex = new Regex("v_r_(hk[0-9]{5})=\"100~(.+)~\"");
                Match m = regex.Match(line);
                if (m.Success)
                {
                    string res = m.Groups[2].Value;
                    string[] items = res.Split(new char[] { '~' });

                    string StockId = m.Groups[1].Value;
                    string name = items[0].Replace("'", "''");
                    double zongshizhi = double.Parse(items[43]);
                    double pe = double.Parse(items[38]);
                    if (zongshizhi > 0.000001)       //是0的全部是退市的股票
                    {
                        var stockInfo = new StockInfo();
                        stockInfo.stockId = StockId;
                        stockInfo.name = name;
                        stockInfo.pe = pe;
                        stockInfo.wholeValue = zongshizhi;

                        if (!Utils.DB_Client.Update<StockInfo>(new
                        {
                            name = name,
                            pe = pe,
                            wholeValue = zongshizhi,

                        }, new { stockId = StockId }))
                        {
                            Utils.DB_Client.Insert<StockInfo>(stockInfo);
                        }
                    }
                }
            }
        }


        private string[] get_stock_items(string prefix,int from,int to,int numCnt)
        {
            try
            {
                DownBase db = new DownBase();
                string[] lines = null;
                var content = "";
                int segment = 50;
                for (int j = from; j < to;  )
                {
                    var all = @"http://qt.gtimg.cn/q=";
                    for (int i = 1; i <= segment; i++)
                    {
                        
                        var formate = "{0:";
                        for (int x = 0; x < numCnt; x++)
                        {
                            formate += "0";
                        }
                        formate += "},";
                        all += string.Format(prefix + formate, j);
                        j++;
                    }
                    var line = db.DownString(all);
                    LogUtils.Print("下载 " + line );
                    content += line;

                }
                lines = content.Split(new char[] { ';' });
                return lines;
            }
            catch (Exception ex)
            {
                
                throw;
            }
        }

        private void save_items(string [] lines,string match)
        {
            foreach (var line in lines)
            {
                Regex regex = new Regex(match);
                Match m = regex.Match(line);
                if (m.Success)
                {
                    try
                    {
                        string res = m.Groups[2].Value;
                        string[] items = res.Split(new char[] { '~' });

                        string StockId = m.Groups[1].Value;
                        string name = items[0].Replace("'", "''");
                        double zongshizhi = double.Parse(items[43]);
                        double pe = double.Parse(items[38]);
                        if (zongshizhi > 0.000001)       //是0的全部是退市的股票
                        {
                            var stockInfo = new StockInfo();
                            stockInfo.stockId = StockId;
                            stockInfo.name = name;
                            stockInfo.pe = pe;
                            stockInfo.wholeValue = zongshizhi;

                            if (!Utils.DB_Client.Update<StockInfo>(new
                            {
                                name = name,
                                pe = pe,
                                wholeValue = zongshizhi,

                            }, new { stockId = StockId }))
                            {
                                Utils.DB_Client.Insert<StockInfo>(stockInfo);
                            }

                            LogUtils.Print(StockId +",总市值" + zongshizhi + "写入数据库" );

                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
            }
        }

        private void down_shanghai_stock()
        {
            string[] lines = get_stock_items("sh", 600000, 603999,6);
            save_items(lines, "v_(sh[0-9]{6})=\"1~(.+)\"");
        }

        private void down_shenzhen_stock()
        {
            string[] lines = get_stock_items("sz", 0, 3000, 6);
            save_items(lines, "v_(sz[0-9]{6})=\"51~(.+)\"");

            lines = get_stock_items("sz", 300001, 301000, 6);
            save_items(lines, "v_(sz[0-9]{6})=\"51~(.+)\"");
        }

        public void DownAllStockInfo()
        {
            down_shanghai_stock();
            LogUtils.Print("下载上海股票成功");
            down_shenzhen_stock();
            LogUtils.Print("下载深圳股票成功");
            down_hongkong_stock();
            LogUtils.Print("下载香港股票成功");
        }


    }
}
